package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import com.itheima.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Arrays;
import java.util.List;

@RequestMapping("/sysUser")
@RestController
@Slf4j
public class SysUserController {
     @Reference
     SysUserService sysUserService;

     //新增弹框回显用户角色
    @RequestMapping("/findRoles")
    public Result findRoles() {
        log.debug("************新增弹框回显用户角色************");
        log.debug("SysUserController：findRoles方法执行了！");
        //调用方法
        List<Role> roleList = sysUserService.findRoles();
        log.debug("SysUserController:roleList:{"+roleList+"}");
        log.debug("************新增弹框新增功能成功************");
        return new Result(true,"查询角色信息成功！！",roleList);
    }

    //新增弹框新增功能
    @RequestMapping("/add")
    public Result add(Integer[] roleIds, @RequestBody SysUser sysUser) {
        log.debug("************新增弹框新增功能************");
        log.debug("SysUserController:roleIds{"+roleIds+"}");
        log.debug("SysUserController:sysUser{"+sysUser+"}");

        String password = sysUser.getPassword();
        if (password == null) {
            return new Result(false,"密码不能为空！！");
        }
        //校验用户角色为必填项
        if (roleIds==null || roleIds.length==0) {
            return new Result(false,"用户角色不能为空！！");
        }
        //调用方法
        sysUserService.add(roleIds,sysUser);
        log.debug("************新增弹框新增功能成功************");
        return new Result(true,"添加成功！！");
    }

    //校验用户名
    @RequestMapping("/checkUsername")
    public Result checkUsername(@RequestBody SysUser sysUser) {
        //校验姓名、密码为必填项
        String username = sysUser.getUsername();
        //调用方法
        Boolean booleanUsername = sysUserService.checkUsername(username);
        if (!booleanUsername) {
            return new Result(false,"用户名被占用!!");
        }
        return new Result(true,"用户名填写成功！！");
    }

    //校验电话号码
    @RequestMapping("/checkTelephone")
    public Result checkTelephone(@RequestBody SysUser sysUser) {
        log.debug("************校验电话号码************");
        //校验姓名、密码为必填项
        String telephone = sysUser.getTelephone();
        //调用方法
        Boolean booleanTelephone = sysUserService.checkTelephone(telephone);
        if (!booleanTelephone) {
            return new Result(false,"电话号码被占用!!");
        }
        log.debug("************校验电话号码完成************");
        return new Result(true,"电话号码填写成功！！");
    }

    //分页功能
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.debug("************分页功能************");
        log.debug("SysUserController:queryPageBean{"+queryPageBean+"}");
        //调用方法
        PageResult pageResult =sysUserService.findPage(queryPageBean);
        log.debug("SysUserController:pageResultList{"+pageResult+"}");
        log.debug("************分页功能完成************");
        return pageResult;
    }

    //编辑功能:回显用户
    @RequestMapping("/edit")
    public Result edit(Integer id) {
        log.debug("************编辑功能:回显用户************");
        log.debug("SysUserController:edit{"+id+"}");
        //调用方法
       SysUser sysUser = sysUserService.edit(id);
        log.debug("SysUserController:sysUser{"+sysUser+"}");
        log.debug("************编辑功能:回显用户************");
        return new Result(true,"用户回显成功！",sysUser);
    }

    //编辑功能:回显关联角色
    @RequestMapping("/findRoleIdsBySysUserId")
    public Result findRoleIdsBySysUserId(Integer id) {
        log.debug("************编辑功能：回显关联角色************");
        log.debug("SysUserController:findRoleIdsBySysUserId{"+id+"}");
        //调用方法
        List<Integer>roleIds = sysUserService.findRoleIdsBySysUserId(id);
        log.debug("SysUserController:sysUser{"+roleIds+"}");
        log.debug("************编辑功能：回显关联角色************");
        return new Result(true,"用户回显成功！",roleIds);
    }

    //编辑修改
    @RequestMapping("/update")
    public Result update(Integer[] roleIds, @RequestBody SysUser sysUser) {
        log.debug("************编辑功能:修改************");
        log.debug("SysUserController:roleIds{"+ Arrays.toString(roleIds)+"}");
        log.debug("SysUserController:sysUser{"+sysUser.toString()+"}");
        //调用方法
        sysUserService.update(roleIds,sysUser);
        log.debug("************编辑功能:修改************");
        return new Result(true,"修改成功！");
    }

    //删除功能
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        log.debug("************删除功能************");
        log.debug("SysUserController:sysUserId{"+id+"}");
        //1.获取登录用户的id
        //1.1获取安全框架上下文对象
        SecurityContext context = SecurityContextHolder.getContext();
        //1.2获取认证对象
        Authentication authentication = context.getAuthentication();
        //1.3获取principal重要对象
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            User user=(User)principal;
            String username = user.getUsername();
            log.debug("获取的登录名："+username);
            //调用service方法
            Integer sysUserId = sysUserService.findByUsername(username);
            //判断id是否一样
            if (id == sysUserId) {
                return new Result(false,"不能删除自己!!");
            }
        }
        //2.调用方法
        sysUserService.delete(id);
        log.debug("************删除功能完成************");
        return new Result(true,"删除成功！");
    }
}
